package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Result;
import com.example.entity.College;
import com.example.entity.ZyzActivity;
import com.example.entity.ZyzAdmin;
import com.example.entity.ZyzSignUp;
import com.example.entity.vo.ZyzSignUpVo;
import com.example.service.CollegeService;
import com.example.service.ZyzActivityService;
import com.example.service.ZyzAdminService;
import com.example.service.ZyzSignUpService;
import com.github.pagehelper.PageInfo;
//import javafx.print.Collation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报名控制器
 */
@RestController
@RequestMapping("/api/zyzSignUp")
public class ZyzSignUpController {

    @Autowired
    private ZyzSignUpService zyzSignUpService;
    @Autowired
    private ZyzAdminService zyzAdminService;
    @Autowired
    private ZyzActivityService zyzActivityService;
    @Autowired
    private CollegeService collegeService;

    /**
     * 报名(添加)
     */
    @PostMapping("/add")
    public Result add(@RequestBody ZyzSignUp zyzSignUp) {
        zyzSignUpService.add(zyzSignUp);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @PostMapping("/delete/batch")
    public Result deleteBatch(String ids) {
        int i = zyzSignUpService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(ZyzSignUp zyzSignUp) {
        List<Map<String, Object>> list =zyzSignUpService.selectAll(zyzSignUp);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(ZyzSignUp zyzSignUp,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Map<String, Object>> page = zyzSignUpService.selectPage(zyzSignUp, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 查询学生的志愿时长和积分
     */
    @GetMapping("/selectPoints")
    public Result selectPoints(Integer studentId) {
        Map<String, Object> map = zyzSignUpService.selectPoints(studentId);
        return Result.success(map);
    }

    /**
     * 公告通知
     */
    @GetMapping("/selectNotice")
    public Result selectNotice(Integer studentId) {
        List<ZyzSignUp> list = zyzSignUpService.selectNotice(studentId);
        List<Map<String, Object> > list1 = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (ObjectUtil.isNotEmpty(list)){
            for (ZyzSignUp zyzSignUp : list) {
                Integer activityId = zyzSignUp.getActivityId();
                ZyzActivity zyzActivity = zyzActivityService.selectById(activityId);
                if ("已结束".equals(zyzActivity.getStatus())){
                    Map<String, Object> stringObjectHashMap = new HashMap<>();
                    String name = zyzActivity.getName()+"活动已结束，您已获得" + zyzActivity.getPoints() + "积分";
                    Date activityStart = zyzActivity.getActivityStart();
                    String format = simpleDateFormat.format(activityStart);
                    stringObjectHashMap.put("name",name);
                    stringObjectHashMap.put("time",format);
                    list1.add(stringObjectHashMap);
                }
            }
        }
        return Result.success(list1);
    }

    /**
     * 积分明细
     */
    @GetMapping("/selectPointsDetail")
    public Result selectPointsDetail(Integer studentId) {
        List<ZyzSignUp> list = zyzSignUpService.selectNotice(studentId);
        List<Map<String, Object> > list1 = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (ObjectUtil.isNotEmpty(list)){
//            int i = 0;
            int j = 0;
            /*for (ZyzSignUp zyzSignUp : list) {
                Map<String, Object> hashMap = new HashMap<>();
                ZyzActivity zyzActivity = zyzActivityService.selectById(zyzSignUp.getActivityId());
                Date activityEnd = zyzActivity.getActivityEnd();
                if ("已结束".equals(zyzActivity.getStatus())){
                    String format = simpleDateFormat.format(activityEnd);
                    hashMap.put("name",zyzActivity.getName());
                    hashMap.put("time",format);
                    hashMap.put("points",zyzActivity.getPoints());
                    hashMap.put("acticityId",zyzSignUp.getActivityId());
//                    if (i == 0){
//                        hashMap.put("before",i);
//                    }else{
//                        hashMap.put("before",j+zyzActivity.getPoints());
//                    }
                    list1.add(hashMap);
                }
            }*/
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> hashMap = new HashMap<>();
                ZyzActivity zyzActivity = zyzActivityService.selectById(list.get(i).getActivityId());
                Date activityEnd = zyzActivity.getActivityEnd();
                if ("已结束".equals(zyzActivity.getStatus())){
                    String format = simpleDateFormat.format(activityEnd);
                    hashMap.put("name",zyzActivity.getName());
                    hashMap.put("time",format);
                    hashMap.put("points",zyzActivity.getPoints());
                    hashMap.put("acticityId",list.get(i).getActivityId());
                    if (i == 0){
                        hashMap.put("before",i);
                    }else{

                        ZyzActivity zyzActivity1 = zyzActivityService.selectById(list.get(i - 1).getActivityId());
                        j += zyzActivity1.getPoints();
                        hashMap.put("before",j);
                    }
                    hashMap.put("after",j+zyzActivity.getPoints());
                    list1.add(hashMap);
                }
            }
        }
        return Result.success(list1);
    }

    /**
     * 查询所有学院
     */
    @GetMapping("/selectAllCollege")
    public Result selectAllCollege() {
        List<College> list = collegeService.selectAll(new College());
        return Result.success(list);
    }

    /**
     * 查询每个学院的报名人数
     */
    @GetMapping("/selectCollegeCount")
    public Result selectCollegeCount(Integer activityId) {
        List<Map<String,Integer>> list1 = collegeService.selectCollegeCount(activityId);
        return Result.success(list1);
    }

    /**
     * 查询年级报名情况
     */
    @GetMapping("/selectGradeCount")
    public Result selectGradeCount(Integer activityId) {
        List<Map<String, Integer>> list = collegeService.selectGradeCount(activityId);
        return Result.success(list);
    }

    /**
     * 查询每个学生参与活动的记录
     */
    @GetMapping("/selectStudentActivity")
    public Result selectStudentActivity(Integer studentId) {
        List<Map<String, Object>> list = zyzSignUpService.selectStudentActivity(studentId);
        return Result.success(list);
    }

}
